<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .test {
        width: 500px;
        height: 500px;
        margin-top: 1900px;
        background-color: #0094ff;
      }
    </style>
  </head>
  <body>
    <img class="test" ></img>
    <script>
      // 定义观察器的配置信息
      let option = {
        threshold: [0, 0.5],
      };
      // 创建实例
      let obserConstr = new IntersectionObserver(funcall, option);

      // 定义回调函数
      function funcall(entries, observer) {
        // 遍历交叉信息
        entries.forEach((entry) => {
          console.log(entry);
          if (entry.isIntersecting) {
            entry.target.src="./head.png"
            entry.target.style.backgroundColor = "#f58f98";
          } else {
            entry.target.style.backgroundColor = "red";
          }
        });
      }
      let ob = document.querySelector(".test");
      obserConstr.observe(ob);
    </script>
  </body>
</html>
